package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.QuestionnaireDetailAdapter;
import cn.otrue.patienthealthmanager.bean.Questions.Question;
import cn.otrue.patienthealthmanager.bean.Questions.QuestionResearchDetail;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.view.NoScrollListView;

import com.google.gson.reflect.TypeToken;

/**
 * 回答问卷和查看问卷
 */
public class QuestionnaireDetailActivity extends BaseActivity {

	private NoScrollListView nslv_question_item_select;
	private QuestionnaireDetailAdapter adapter;// 适配器
	private ArrayList<String> spinnerData;
	private TextView tv_question_item_wenti;// 标题
	// private String useraccount;
	private List<QuestionResearchDetail> questionTemplateQuestionList;
	private String researchId;
	private String isSelected;
	private List<Question> question;
	private TextView tv_title_other;
	public List<Map<String, String>> problemSeleteditem;// 回答问卷时的问题保存
	public Map<String, String> seleteditem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_questionnaire_detail);
		initView();
	}

	private void initView() {
		initTitleBar();
		// useraccount = getIntent().getStringExtra("useraccount");
		tv_question_item_wenti = (TextView) findViewById(R.id.tv_question_item_wenti);
		nslv_question_item_select = (NoScrollListView) findViewById(R.id.nslv_question_item_select);
		question = new ArrayList<Question>();
		questionTemplateQuestionList = new ArrayList<QuestionResearchDetail>();
		researchId = getIntent().getStringExtra("researchId");
		getData();

	}

	/**
	 * 获取问卷明细
	 */
	private void getData() {
		HashMap<String, Object> hash = new HashMap<String, Object>();
		hash.put("researchId", researchId);
		TransferParameter parameter = new TransferParameter();
		parameter.setData(hash);
		new HttpServer().post(HttpCons.AQuestionnaireDetailedQuery, parameter,
				handler);
	}

	private Handler handler = new ServerHandler() {

		public void onSuccess(Bundle data, String state, String msg) {
			String datas = data.getString(HttpCons.DATA);
			if ("001".equals(state)) {
				if (!("".equals(datas))) {
					question = HttpServer.gson.fromJson(datas,new TypeToken<List<Question>>() {}.getType());
					questionTemplateQuestionList = question.get(0).questionResearchDetailList;
					isSelected = question.get(0).isSelected;
					adapter = new QuestionnaireDetailAdapter(QuestionnaireDetailActivity.this,questionTemplateQuestionList, isSelected);
					nslv_question_item_select.setAdapter(adapter);
					tv_question_item_wenti.setText(question.get(0).questionTitle);
				} else {
					Toast.makeText(QuestionnaireDetailActivity.this, msg,Toast.LENGTH_SHORT).show();
				}
				if ("1".equals(isSelected)) {
					tv_title_other.setVisibility(View.GONE);
					tv_title_other.setEnabled(false);
				}
			} else {
				Toast.makeText(QuestionnaireDetailActivity.this, msg,Toast.LENGTH_SHORT).show();
			}
		};

		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(QuestionnaireDetailActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
				Toast.makeText(getApplicationContext(), "登录失败", 0).show();
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)
			}
		};
	};

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {

		case R.id.tv_title_back:
			finish();
			break;
		case R.id.tv_title_other:
			setData();
			break;
		default:
			break;
		}
	}

	/**
	 * 回答问卷
	 */
	public void setData() {
		problemSeleteditem = new ArrayList<Map<String, String>>();
		HashMap<String, Object> hash = new HashMap<String, Object>();
		for (int i = 0; i < questionTemplateQuestionList.size(); i++) {
			for (int j = 0; j < questionTemplateQuestionList.get(i).questionResearchDetailSeleteditemList.size(); j++) {
				seleteditem = new HashMap<String, String>();
				if (questionTemplateQuestionList.get(i).questionResearchDetailSeleteditemList.get(j).isChecked==1) {
					seleteditem.put("seleteditem_id",questionTemplateQuestionList.get(i).questionResearchDetailSeleteditemList.get(j).seleteditem_id);
					problemSeleteditem.add(seleteditem);
				}
			}
		}
		hash.put("setProblem", problemSeleteditem);
		hash.put("researchId", researchId);
		TransferParameter parameter = new TransferParameter();
//		Gson gson=new Gson();
//		String json=gson.toJson(hash);
//		json=json;
		parameter.setData(hash);
		new HttpServer().post(HttpCons.UpdateIsChecked, parameter,answerHandler);
	}

	Handler answerHandler = new ServerHandler() {
		public void onSuccess(Bundle data, String state, String msg) {
			if("001".equals(state)){
				Toast.makeText(QuestionnaireDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
				if(QuestionnaireList.list!=null){
					QuestionnaireList.list.finish();
				}
				Intent questionlist=new Intent(QuestionnaireDetailActivity.this, QuestionnaireList.class);
				startActivity(questionlist);
				finish();
			}else{
				Toast.makeText(QuestionnaireDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
			}
		};

		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(QuestionnaireDetailActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
				Toast.makeText(getApplicationContext(), "登录失败", 0).show();
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)
			}
		};
	};

	private void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.show_questionnaire));
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		tv_title_other = (TextView) findViewById(R.id.tv_title_other);
		tv_title_other.setVisibility(View.VISIBLE);
		tv_title_other.setText(getText(R.string.community_fatie));
		tv_title_other.setOnClickListener(this);
	}
}
